package com.slingshot.identitysdk.common.view

import android.arch.lifecycle.Observer
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.slingshot.identitysdk.R
import com.slingshot.identitysdk.viewmodel.SlingshotViewModel
import kotlinx.android.synthetic.main.view_main_top.view.*

/**
 * @author liu shuai
 * 描述：顶部视图
 */
class MainTopView(context: Context, attributeSet: AttributeSet) :
    LinearLayout(context, attributeSet) {

    init {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.view_main_top, this)
    }

    fun init(activity: AppCompatActivity, slingshotViewModel: SlingshotViewModel) {
        slingshotViewModel.getSlingshotData().observe(activity, Observer {
            run {
                it?.let {
                    when (it.state) {
                        0 -> {
                            topDefaultView.visibility = View.VISIBLE
                            userPhotoView.visibility = View.GONE
                            userContentView.visibility = View.GONE
                        }
                        1 -> {
                            topDefaultView.visibility = View.GONE
                            userPhotoView.visibility = View.VISIBLE
                            userContentView.visibility = View.VISIBLE
                            userContentView.setTextColor(
                                ContextCompat.getColor(
                                    context,
                                    R.color.color_theme_white
                                )
                            )

                            userPhotoView.setImageBitmap(it.bitmap)
                            userContentView.text = it.content
                        }
                        2 -> {
                            topDefaultView.visibility = View.GONE
                            userPhotoView.visibility = View.VISIBLE
                            userContentView.visibility = View.VISIBLE
                            userContentView.setTextColor(
                                ContextCompat.getColor(
                                    context,
                                    R.color.color_theme_red
                                )
                            )

                            userPhotoView.setImageBitmap(null)
                            userContentView.text = it.content
                        }
                    }
                }
            }
        })
    }
}